package com.bootdo.system.controller;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.controller.BaseController;
import com.bootdo.company.service.CompanyService;
import com.bootdo.company.service.PostService;
import com.bootdo.system.service.SystemService;
import com.bootdo.utils.ResultUtil;
import com.bootdo.utils.ResultVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 部门管理
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-27 14:40:36
 */

@Controller
@RequestMapping("/stat")
public class StatsController extends BaseController {
    @Autowired
    CompanyService companyService;
    @Autowired
    PostService postService;
    @Autowired
    SystemService systemService;


    @PostConstruct
    public void init() {
        Map<String, Object> map = systemService.select();
        if (map == null || map.size() == 0) {
            return;
        }
        visitTotal = (int) map.get("total_visit");
        visitPostList = (List<Map<String, Object>>) JSON.parse((String) map.get("vist_rewards"));
    }

    private static List<Map<String, Object>> visitPostList = new FixSizeLinked<>(7);
    private static int visitTotal = 0;

    public synchronized static void addVisitTotal() {
        visitTotal++;
    }

    private static String getName(String name) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(name, 0, 1);
        for (int i = 0; i < name.length() - 1; i++) {
            stringBuilder.append("*");
        }
        return stringBuilder.toString();
    }

    public synchronized static void addVistPost(String name, String job) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", getName(name));
        map.put("jobName", job);
        visitPostList.add(map);
    }

    private synchronized void saveData() {
        String str = JSON.toJSONString(visitPostList);
        Map map = new HashMap();
        map.put("visitReward", str);
        map.put("visitTotal", visitTotal);
        systemService.insert(map);
    }


    @ApiOperation(value = "统计", notes = "")
    @ResponseBody
    @GetMapping("/system")
    public ResultVO system(Map<String, Object> query) {
        StatsController.addVisitTotal();
        int companyTotal = companyService.count(new HashMap<>());
        int postTotal = postService.count();
        Map<String, Object> map = new HashMap<>();
        map.put("companyTotal", companyTotal);
        map.put("postTotal", postTotal);
        map.put("visitTotal", visitTotal);
        map.put("visitReward", visitPostList);
        saveData();
        return ResultUtil.success(map);
    }
}

class FixSizeLinked<T> extends LinkedList<T> {
    private static final long serialVersionUID = 3292612616231532364L;
    // 定义缓存的容量
    private int capacity;

    FixSizeLinked(int capacity) {
        super();
        this.capacity = capacity;
    }

    @Override
    public boolean add(T e) {
        // 超过长度，移除最后一个
        if (size() + 1 > capacity) {
            super.removeFirst();
        }
        return super.add(e);
    }
}
